void DrawClock()
{
  switch (Pos) {
  case 0:
    DrawSevenSegment(disNumbers[minutesLittle], Pos, secondsTick);
    break;
  case 1:
    DrawSevenSegment(disNumbers[minutesBig], Pos, false);
    break;
  case 2:
    DrawSevenSegment(disNumbers[hoursLittle], Pos, true);
    break;
  case 3:
    if(hoursBig == 0)
    {
      DrawSevenSegment( disLetters[0], Pos, false); // Dont draw 0 for leading hour
    }
    else
    {
      DrawSevenSegment(disNumbers[hoursBig], Pos, false);
    }
    break;
  }

}

void ClearScreen()
{
  digitalWrite(positions[0], LOW);
  digitalWrite(positions[1], LOW);
  digitalWrite(positions[2], LOW);
  digitalWrite(positions[3], LOW);
}

void DrawMenuAlarm()
{
  switch (Pos) {
  case 0:
    DrawSevenSegment(disNumbers[alarmminutesLittle], Pos, secondsTick);
    break;
  case 1:
    DrawSevenSegment(disNumbers[alarmminutesBig], Pos, false);
    break;
  case 2:
    DrawSevenSegment(disNumbers[alarmhoursLittle], Pos, true);
    break;
  case 3:
    DrawSevenSegment(disNumbers[alarmhoursBig], Pos, false);
    break;
  }

}

void DrawAlarm()
{
  if(showAlarmFlop || writing)
  {
    writing = true;
    DrawScrollingText(alarmTexts[alarmText], alarmTextsSize[alarmText]);
    if(!writing)
    {
      lastAlarmTick =  millis();
      showAlarmFlop = false;
    }
  }
  else
  {
    DrawClock();
  }
  
  if(lastAlarmTick == 0)
  {
    lastAlarmTick = millis();
  }
  else if (millis() - lastAlarmTick >= alarmFlashRate) 
  {
    lastAlarmTick =  millis();
    showAlarmFlop = !showAlarmFlop;
  }
}

bool* GetSegmentFromChar(char character)
{
  return disLetters[character - ASCIIOffset];
}

void WriteTextAlways(char text[], int size)
{
    textSize = size;//sizeof(text);
    textToWrite = text;
    charPos = 0;
    writing = true;
}

bool WriteText(char text[], int size)
{
  if(writing == false)
  {
    textSize = size;//sizeof(text);
    textToWrite = text;
    charPos = 0;
    writing = true;
    return true;
  }

  return false;
}

bool isEnd(char c)
{
  return c == 0;
}

void DrawScrollingText(char* text, int size)
{
  int textLength = size;//sizeof(text);
  
  if(charPos == 0)
  {
    //lastScrollingTick = millis();
    //writing = true;
  }
  switch (Pos) {
  case 0:
    if(charPos >= 0 && charPos <= textLength)
    {
      DrawSevenSegment(GetSegmentFromChar(text[charPos]), Pos, false);
    }
    break;
  case 1:
    if(charPos >= 1 && charPos <= textLength + 1)
    {
      DrawSevenSegment(GetSegmentFromChar(text[charPos - 1]), Pos, false);
    }
    break;
  case 2:
    if(charPos >= 2 && charPos <= textLength + 2)
    {
      DrawSevenSegment(GetSegmentFromChar(text[charPos - 2]), Pos, false);
    }
    break;
  case 3:
    if(charPos >= 3 && charPos <= textLength + 3)
    {
      DrawSevenSegment(GetSegmentFromChar(text[charPos - 3]), Pos, false);
    }
    break;
  }
  
  if(charPos > textLength + 3)
  {
    charPos = 0;
    writing = false;
    lastScrollingTick = 0;
  }


  if(lastScrollingTick == 0)
  {
    lastScrollingTick = millis();
  }
  else if (millis() - lastScrollingTick >= textScrollRate) 
  {
    lastScrollingTick =  millis();
    charPos++;
  }
}



void DrawSevenSegment(bool display[], int position, bool Decimal)
{
  digitalWrite(positions[0], LOW);
  digitalWrite(positions[1], LOW);
  digitalWrite(positions[2], LOW);
  digitalWrite(positions[3], LOW);

  for(int i = 0; i <= 7; i++)
  {
    digitalWrite(segments[i], !display[i]);
  }

   digitalWrite(segDp, !Decimal);
  //digitalWrite(13, !display[9]);

  digitalWrite(positions[position], HIGH);
}

